package tech.lq0.dreamaticvoyage.entity.projectile;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import tech.lq0.dreamaticvoyage.init.EntityRegistry;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;

public class OminousWaveEntity extends ThrowableItemProjectile {

    public static final EntityDataAccessor<Integer> TYPE = SynchedEntityData.defineId(OminousWaveEntity.class, EntityDataSerializers.INT);

    public int type;
    public float damage = 10f;
    public int life = 10;

    public OminousWaveEntity(Level pLevel, LivingEntity entity) {
        this(pLevel, entity, 0);
    }

    public OminousWaveEntity(Level level, LivingEntity entity, int type) {
        super(EntityRegistry.OMINOUS_WAVE_ENTITY.get(), entity, level);
        this.entityData.set(TYPE, type);
        this.type = type;
    }

    public OminousWaveEntity(EntityType<? extends ThrowableItemProjectile> pEntityType, Level pLevel) {
        super(pEntityType, pLevel);
    }

    @Override
    public void addAdditionalSaveData(CompoundTag pCompound) {
        super.addAdditionalSaveData(pCompound);
        pCompound.putFloat("Damage", this.damage);
        pCompound.putInt("Life", this.life);
    }

    @Override
    public void readAdditionalSaveData(CompoundTag pCompound) {
        super.readAdditionalSaveData(pCompound);
        this.damage = pCompound.getFloat("Damage");
        this.life = pCompound.getInt("Life");
    }

    @Override
    public boolean isOnFire() {
        return false;
    }

    @Override
    protected void defineSynchedData() {
        super.defineSynchedData();
        this.entityData.define(TYPE, this.type);
    }

    @Override
    public void tick() {
        super.tick();
        --this.life;
        if (this.life <= 0) {
            this.discard();
        }
    }

    @Override
    protected void onHitEntity(EntityHitResult pResult) {
        Entity entity = pResult.getEntity();
        if (entity instanceof LivingEntity livingEntity) {
            float damage = this.damage;
            if (this.getEntityData().get(TYPE) == 1) {
                damage *= 3;
            }

            if (this.getOwner() instanceof Player player) {
                livingEntity.hurt(this.damageSources().playerAttack(player), damage);
            } else {
                livingEntity.hurt(this.damageSources().thrown(this, this.getOwner() instanceof LivingEntity living ? living : null), damage);
            }
        }
    }

    @Override
    protected void onHitBlock(BlockHitResult pResult) {
        super.onHitBlock(pResult);
    }

    @Override
    protected Item getDefaultItem() {
        return ItemRegistry.OMINOUS_SICKLE.get();
    }

    public OminousWaveEntity damage(float damage) {
        this.damage = damage;
        return this;
    }

    @Override
    protected float getGravity() {
        return 0.0f;
    }
}
